//
//  BLLocalize.m
//  Benz.L
//
//  Created by Benz.L on 23/08/2018.
//  Copyright © 2018 BENZL. All rights reserved.
//

#import "BLLocalize.h"

BLLocalizeLanguageChangedNotification const BLLocalizeLanguageChangedNotificationName = @"BLLocalizeLanguageChangedNotificationName";

BLLocalizeLanguage const BLLocalizeLanguageSystem  = @"system";
BLLocalizeLanguage const BLLocalizeLanguageEnglish = @"en";
BLLocalizeLanguage const BLLocalizeLanguageChineseHans = @"zh-Hans";
BLLocalizeLanguage const BLLocalizeLanguageChineseHant = @"zh-Hant";

@interface BLLocalize() {
}
@property(nonatomic, strong) NSString *lprojPath;
@end
@implementation BLLocalize

+ (instancetype)localize {
    static BLLocalize *localize = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        localize = [[BLLocalize alloc] init];
    });
    return localize;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.language = self.defaultLanguage;
    }
    return self;
}

- (NSString *)localizedStringForKey:(NSString *)key {
    NSBundle *bundle = [NSBundle bundleWithURL:[NSURL fileURLWithPath:self.lprojPath]];//[NSBundle bundleWithPath:self.lprojPath];
    NSError *error = nil;
    [bundle loadAndReturnError:&error];
    NSString *result = [bundle localizedStringForKey:key value:nil table:@"Localizable"];//如果本地化文件不是以Localizable命名，比如language.strings，那么table就传入‘language’
    return result?:key;
}

- (NSString *)localizedStringForKeys:(NSString *)key, ... NS_REQUIRES_NIL_TERMINATION
{
    NSMutableString* result = [[NSMutableString alloc] init];
    
    va_list list;
    va_start(list, key);
    do {
        [result appendString:[self localizedStringForKey:key]];
        key = va_arg(list, NSString*);
    } while (key);
    va_end(list);
    
    return [result description];
}

- (void)setLanguage:(BLLocalizeLanguage)language {
    /*
    if ([language isEqual:BLLocalizeLanguageSystem]) {
        if (![_language isEqual:self.defaultLanguage]) {
            _language = self.defaultLanguage;
            self.lprojPath = [[NSBundle mainBundle] pathForResource:_language ofType:@"lproj"];
        }
    }
    else {
        if (!_language || ![language isEqual:_language]) {
            _language = language;
            self.lprojPath = [[NSBundle mainBundle] pathForResource:_language ofType:@"lproj"];
        }
    }
     */
    
    _language = language;
    if ([language isEqual:BLLocalizeLanguageSystem]) {
        _language = self.defaultLanguage;
    }
    self.lprojPath = [[NSBundle mainBundle] pathForResource:_language ofType:@"lproj"];
    [[NSNotificationCenter defaultCenter] postNotificationName:BLLocalizeLanguageChangedNotificationName object:nil];
}

- (BLLocalizeLanguage)defaultLanguage {
    NSString *lan = (NSString *)NSLocale.preferredLanguages.firstObject;
    if ([lan rangeOfString:BLLocalizeLanguageChineseHans].length) {
        return BLLocalizeLanguageChineseHans;
    }
    else if ([lan rangeOfString:BLLocalizeLanguageChineseHant].length) {
        return BLLocalizeLanguageChineseHant;
    }
    else if ([lan rangeOfString:BLLocalizeLanguageEnglish].length) {
        return BLLocalizeLanguageEnglish;
    }
    else {
        return lan;
    }
}

- (NSString *)lprojPath {
    if (!_lprojPath) {
        self.lprojPath = [[NSBundle mainBundle] pathForResource:BLLocalizeLanguageEnglish ofType:@"lproj"];
    }
    return _lprojPath;
}

- (BOOL)isEnglish {
    return [_lprojPath hasSuffix:@"en.lproj"];
}

@end
